package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.jopo.Emp;
import com.itheima.jopo.PageBean;
import com.itheima.mapper.EmpMapper;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * EmpServiceImpl
 *
 * @author liliudong
 * @version 1.0
 * @description
 * @date 2023/4/29 16:15
 */
@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;

    //  参数 方法体  返回值

    // 结构来讲 重复，重构，封装
    @Override
    public PageBean page(Integer page, Integer pageSize, String name, Integer gender, LocalDate begin, LocalDate end) {
        PageHelper.startPage(page, pageSize);
        // 员工数据
        List<Emp> empList = empMapper.selectList(name, gender, begin, end);
        // 强转不优雅
        // Page<Emp> empPage = (Page<Emp>) empList;
        // 背下来
        PageInfo<Emp> empPageInfo = new PageInfo<>(empList);

        // 封装
        PageBean pageBean = new PageBean();
        pageBean.setRows(empPageInfo.getList());
        pageBean.setTotal(empPageInfo.getTotal());
        return pageBean;
    }

    @Override
    public void removeByIds(List<Integer> ids) {
        empMapper.deleteByIds(ids);
    }

    @Override
    public void addOne(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.insertOne(emp);
    }

    @Override
    public Emp getById(Integer id) {
        return empMapper.selectById(id);
    }

    @Override
    public void updateEmp(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);
    }
}
